<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="dataForm.keyWord" placeholder="类型名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @refreshDataList="getDataList" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="width: 100%;" v-loading="dataListLoading">
      <el-table-column type="index" width="50" label="排序"></el-table-column>
      <el-table-column align="center" prop="type" header-align="center" min-width="150" label="类型"></el-table-column>
      <el-table-column
        align="center"
        prop="description"
        header-align="center"
        min-width="150"
        label="描述"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="remarks"
        header-align="center"
        min-width="150"
        label="备注信息"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="createTime"
        header-align="center"
        min-width="150"
        label="创建时间"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="updateTime"
        header-align="center"
        min-width="150"
        label="更新时间"
      ></el-table-column>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="delOrUpdateHandle(scope.row.id)">删除</el-button>
          <!-- <el-button
            type="text"
            size="small"
            @click="seeOrUpdateHandle(scope.row.id,scope.row.type)"
          >字典项</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!--    弹窗，新增/修改-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <see-or-update v-if="seeOrUpdateVisible" ref="seeOrUpdate" @refreshDataList="getDataList"></see-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './dict-add'
import SeeOrUpdate from './dict-dictionaries'
export default {
  name: 'notice',
  data() {
    return {
      dataForm: {
        keyWord: ''
      },
      dataList: [],
      dataListSelections: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addOrUpdateVisible: false,
      editOrUpdateVisible: false,
      dataListLoading: false,
      seeOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    SeeOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.$http({
        url: '/dict/page',
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize,
          keyword: this.dataForm.keyWord
        }
      }).then(data => {
        if (data) {
          this.dataList = data.data.list
          this.totalPage = data.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    delOrUpdateHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id
          })
      this.$confirm(`确定对进行[${ids ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/dict/delete',
          method: 'post',
          data: { ids: ids.join(',') }
        }).then(data => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 字典项
    seeOrUpdateHandle(id, type) {
      this.seeOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.seeOrUpdate.init(id, type)
      })
    },
    editOrUpdateHandle(id) {
      this.editOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.editOrUpdate.init(id)
      })
    }
  }
}
</script>

<style scoped>
</style>
